package com.intellij.GraphEditor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: karpov
 * Date: 19.11.2006
 * Time: 15:53:26
 * To change this template use File | Settings | File Templates.
 */

public class Editor extends Frame {
    Menu MyMenu;
    private JButton openButton;
    private JButton saveButton;
    private JButton exitButton;
    private JPanel MainPanel;
    private JButton nodeButton;
    private JButton edgeButton;
    private JButton deleteButton;
    public JPanel output;
    public JLabel coord;
    private JButton helpButton;
    public JTextArea CommentTextArea;
    String keymsg = "fuck";
    String mousemsg = "";
    //    int mouseX = 30, mouseY = 30;
    static String source_data;

    static Vector vec;

    public static int mode;

    
    

    public Editor() {
        output.addMouseListener(new MyMouseAdapter(this));
        output.addMouseMotionListener(new MyMouseMotionAdapter(this));
        output.addKeyListener(new MyKeyAdapter(this));
        //output.requestFocus();
                    
    }


    public static void main(String[] args) {
        final Editor editor_object = new Editor();
        MyJMenu menu = new MyJMenu(editor_object);
        final JFrame frame = new JFrame("Graph  Editor");
        frame.setJMenuBar(menu.menuBar);
        frame.setContentPane(editor_object.MainPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(800, 600);


        frame.setVisible(true);
        frame.setFocusable(true);


        final help_form subframe = new help_form();
        final JFrame frame1 = new JFrame("Help");

        vec = new Vector();

        editor_object.exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("ACTION_Exit");
                System.exit(0);
            }
        });

        editor_object.openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                FileDialog fd = new FileDialog(frame, "File Loading");
                fd.setVisible(true);
                try {
                    source_data = Loading_file(fd.getFile());
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                System.out.printf("action_OPEN_performed" + source_data);
            }
        });
        editor_object.saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("action_SAVE");
                //           Graphics g = editor_object.output.getGraphics();
                //           editor_object.outpaint(g);
            }
        });
        editor_object.nodeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("ACTION_Node");
                mode = 1;
            }
        });
        editor_object.edgeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("ACTION_Node");
                mode = 2;
            }
        });
        editor_object.deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("ACTION_Node");
                mode = 3;
            }
        });
        editor_object.helpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf("ACTION_HELP");


                frame1.setContentPane(subframe.Panel);
                // frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.pack();

                frame1.setVisible(true);


            }
        });
        subframe.OKButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.printf(" Close Dialog ");
                frame1.setVisible(false);

            }
        });


    }

    private static String Loading_file(String file_name) throws IOException {
        if (file_name != null) {
            FileReader f1 = new FileReader(file_name);
            BufferedReader br = new BufferedReader(f1);
            String s;
            String temp = "";
            while ((s = br.readLine()) != null) {
                temp += s + "\n";
            }
//                System.out.print("Test info 2 "+temp);
            f1.close();
            return temp;
        } else
            return null;
    }

    public  void repaint_() {
//TODO: check work
        Graphics gr = new Editor().output.getGraphics();
        //      gr.dispose();
        for (int i = 0; i < vec.size(); i++) {
            Node temp = (Node) vec.elementAt(i);
            temp.draw_node();
        }
    }

    //     }
    //TODO: delete method
//    public void outpaint(Graphics g, int X, int Y) {
//        //       g.drawString("BA BLA", 10,40);
//        //      g.drawString(mousemsg,mouseX,mouseY);
//        coord.setText("X:" + X + "  Y:" + Y + "");
//    }


}


